package com.example.demo2.controller;

import com.example.demo2.bean.CommentBean;
import com.example.demo2.service.CommentBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
@CrossOrigin(origins = "*",
        methods = {RequestMethod.GET,RequestMethod.OPTIONS,RequestMethod.POST})
public class CommentBeanController {

    @Autowired
    CommentBeanService commentBeanService;
    //查询
    @RequestMapping("/comment/list")
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        List<CommentBean> list = commentBeanService.list();
        if (list != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("list", list);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    //删除
    @RequestMapping("/commetn/delete")
    public Map<String, Object> delete(@RequestBody CommentBean commentBean) {
        Map<String, Object> map = new HashMap<>();
        try {
            commentBeanService.deleteById(commentBean.getComment_id());
            map.put("success", true);
            map.put("msg", "成功");
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", "失败");
        }

        return map;
    }
    // 修改
    @RequestMapping("/comment/update")
    public Map<String, Object> update(@RequestBody CommentBean commentBean) {
        Map<String, Object> map = new HashMap<>();
        CommentBean commentBean1 = commentBeanService.update(commentBean);
        if (commentBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", commentBean1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    // 添加
    @RequestMapping("/home/addone")
    public Map<String, Object> addone(@RequestBody CommentBean commentBean) {
        Map<String, Object> map = new HashMap<>();
        CommentBean commentBean1 = commentBeanService.addone(commentBean);
        if (commentBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", commentBean1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

}
